package com.groupwork.meeting.service.impl;

import com.groupwork.meeting.Mapper.UserMapper;
import com.groupwork.meeting.Mapper.UserRoleMapper;
import com.groupwork.meeting.entity.User;
import com.groupwork.meeting.entity.UserRole;
import com.groupwork.meeting.service.IUserRoleService;
import com.groupwork.meeting.service.IUserService;
import com.groupwork.meeting.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.UUID;

/**
 * @author 鱼凫
 * @version 1.0
 * Create by 2021/10/28 23:43
 * @Description //TODO $user模块业务层实现类
 */

@Service
public class UserRoleServiceImpl implements IUserRoleService {
    @Autowired
    private UserRoleMapper userRoleMapper;
    private UserMapper userMapper;

    private String getMd5Password(String password, String salt) {
        for (int i = 0; i < 3; i++) {
            password = DigestUtils.md5DigestAsHex((salt + password + salt).getBytes()).toUpperCase();
        }
        return password;
    }

    @Override
    public UserRole login(String username, String password) {
        UserRole result = userRoleMapper.findByUserName(username);
        User userrsult = userMapper.findByUserName(username);
        if (userrsult == null){
            throw new UserNotFoundException("用户数据不存在");
        }else if (result == null){
            throw new UserRoleNotFoundException("角色数据不存在");
        }
        String salt = userrsult.getSalt();
        String md5Password = getMd5Password(password,salt);
        if (!userrsult.getUserPwd().equals(md5Password)){
            throw new PasswordNotMatchException("用户名或密码错误");
        }
        UserRole userRole = new UserRole();
        return userRole;
    }

    @Override
    public UserRole getByUname(String username) {
        UserRole result = userRoleMapper.findByUserName(username);
        if(result == null){
            throw new UserRoleNotFoundException("用户角色数据不存在");
        }
        UserRole userRole = new UserRole();
        return userRole;
    }

}
